package com.th.supcom.pds.impl.service.internal.phamapply.command;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.PdsPatPhamRemain;

/**
 * 获取患者可用药品剩余数量
 * 
 * @author leaqua@qq.com
 */
public class ObtainPatRemainPham extends AbstractPhamApplyManagerBean implements Command
{

    @Override
    public boolean execute (Context arg0) throws Exception
    {
        PhamDispenseContext context = (PhamDispenseContext) arg0;
        List <Long> paiVisitIdList = context.getPaiVisitIdList ();
        List <PdsPatPhamRemain> phamRemainList = this.getInpPatPhamLeftBasicService ().findPatPhamRemainsByPaiVisitIdList (paiVisitIdList);
        Map <Long, List <PdsPatPhamRemain>> remainMapByPat = new HashMap <Long, List <PdsPatPhamRemain>> ();
        // 将剩余药品数量按照患者住院流水号分组
        for (PdsPatPhamRemain pdsPatPhamRemain : phamRemainList)
        {
            List <PdsPatPhamRemain> temp = null;
            if (remainMapByPat.containsKey (pdsPatPhamRemain.getPaiVisitId ()))
            {
                temp = remainMapByPat.get (pdsPatPhamRemain.getPaiVisitId ());
            }
            else
            {
                temp = new ArrayList <PdsPatPhamRemain> ();
            }
            temp.add (pdsPatPhamRemain);
            remainMapByPat.put (pdsPatPhamRemain.getPaiVisitId (), temp);
        }
        // 将患者住院流水号按照药品分则
        Map <Long, Map <String, PdsPatPhamRemain>> remainPham = new HashMap <Long, Map <String, PdsPatPhamRemain>> ();
        Set <Map.Entry <Long, List <PdsPatPhamRemain>>> set = remainMapByPat.entrySet ();
        for (Iterator <Map.Entry <Long, List <PdsPatPhamRemain>>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <Long, List <PdsPatPhamRemain>> entry = (Map.Entry <Long, List <PdsPatPhamRemain>>) it.next ();
            List <PdsPatPhamRemain> remainList = entry.getValue ();
            for (PdsPatPhamRemain pdsPatPhamRemain : remainList)
            {
                Map <String, PdsPatPhamRemain> temp = new HashMap <String, PdsPatPhamRemain> ();
                temp.put (pdsPatPhamRemain.getPhamStdCode (), pdsPatPhamRemain);
                remainPham.put (entry.getKey (), temp);
            }
        }
        context.setPatPhamRemainMap (remainPham);
        return false;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (ObtainPatRemainPham.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
